package io.a.opencv;

import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.OpenCVFrameConverter;

import javax.swing.*;

public class Video {
    public static void main(String[] args) {
        FFmpegFrameGrabber grabber = null;
        try {
            // String file = "rtsp://admin:Zjlab@2022@10.0.108.100:554/Streaming/Channels/101";
            String file = "rtsp://10.10.40.209:8554/1";
            grabber = FFmpegFrameGrabber.createDefault(file);

            grabber.setOption("rtsp_transport", "tcp"); // 使用tcp的方式，不然会丢包很严重
            grabber.setOption("stimeout", "10000000"); // 设置超时时间（单位为微秒）

            // 设置日志级别为调试，以便查看更多信息
            grabber.setOption("loglevel", "debug");
//            grabber.setFrameRate(30);
            grabber.setImageWidth(1920);
            grabber.setImageHeight(1080);

            System.out.println("grabber start");
            grabber.startUnsafe();

            //1.播放视频

            CanvasFrame canvasFrame = new CanvasFrame("摄像机");
            canvasFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            canvasFrame.setAlwaysOnTop(true);
            OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
            while (true) {
                Frame frame = grabber.grabImage();
                canvasFrame.showImage(frame);
            }

        } catch (Exception e) {
            System.out.println(e);
        } finally {

        }
    }
}

